home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2007 December / PCWKCD1207B.iso / Blogowanie poza sfera / Scribefire-1.4.2 / scribefire-1.4.2-fx+fl.xpi / chrome / content / midas.js < prev    next >
Encoding:
JavaScript  |  2007-07-24  |  39.5 KB  |  1,032 lines

  1. /*
  2. Firefox Midas Handling code (Rich text editing)
  3. -----------------------------------------------------
  4.  
  5. A portion of this code is derived from GPL 2.0 code originally by David Murray, author of the outstanding DeepestSender extension (http://deepestsender.mozdev.org)
  6. Original note by David Murray:
  7. This lot of code is half made up from Mozilla.org's Midas demo (http://www.mozilla.org/editor/midasdemo/)
  8. and Netscape DevEdge's demo of the control also (http://devedge.netscape.com/viewsource/2003/midas/01/).
  9. */
  10.  
  11. var gPerFormancingColorPick = false; //Keep color picker from triggering twice (bad hack)
  12. var gPerFormancingSelectedTab = 0;
  13. var gTheTabHTMLEditor = null;
  14. var performancingMidas = new Object();
  15. var performancingTextEdit = new Object();
  16.  
  17. performancingMidas.setUpMidas = function() {
  18.     this.dontautoformat = false;
  19.     var midas = performancingUI.getRichEditor();
  20.     var theSourceBox = document.getElementById("performancing-message-source");
  21.     window.editorShell = midas.editorShell;
  22.     this.makeBlank();
  23.     midas.makeEditable("html", false);
  24.     this.sourceview = false;
  25.     
  26.     //Set Color icon to black. performance-color-picker
  27.     document.getElementById("performance-color-picker").color = "#000000";
  28.     
  29.     //Open Last viewed window
  30.     var lastOpenedEditTab = null;
  31.     try{
  32.         lastOpenedEditTab = gPerformancingUtil.prefs.getCharPref('display.state.lastviewopened');
  33.     }catch(e){
  34.         lastOpenedEditTab = "tab-normal-edit";
  35.     }
  36.     var lastOpenedEditTabObj = document.getElementById(lastOpenedEditTab);
  37.     //performancingMidas.viewTab(lastOpenedEditTabObj);
  38.     //var tabbox = document.getElementById("performancing-editor-tabbox"); 
  39.     //tabbox.selectedIndex = lastOpenedEditTab;
  40.     
  41.     if(lastOpenedEditTab == 0){
  42.         midas.contentWindow.focus();
  43.     }else if(lastOpenedEditTab == 1){
  44.         theSourceBox.focus();
  45.     }
  46.     
  47.     gTheTabHTMLEditor = midas.getHTMLEditor(midas.contentWindow);
  48.     var useCSS = gPerformancingUtil.prefs.getBoolPref("settings.usecss");
  49.     midas.contentDocument.execCommand("styleWithCSS", false, useCSS);
  50.     //gTheTabHTMLEditor.returnInParagraphCreatesNewParagraph = true;
  51. }
  52.  
  53. performancingMidas.makeBlank = function() {
  54.     // This is to blank out the iframe, for if a post has been sent succesfully or whatever.
  55.     var midas = document.getElementById("performancing-message");
  56.     var SourceTextBox = document.getElementById("performancing-message-source");
  57.     var subjectTextBox = document.getElementById("performancing-editor-subject");
  58.     var winPreview = document.getElementById("performancing-preview-display");
  59.     try{
  60.         winPreview.contentWindow.document.body.innerHTML = "";
  61.     }catch(e){}
  62.     SourceTextBox.value = "";
  63.     subjectTextBox.value = "";
  64.     //midas.contentDocument.body.innerHTML = "<p> </p>";
  65.     midas.contentDocument.body.innerHTML = "<br/>";
  66.     midas.contentDocument.designMode = "On";
  67.     
  68.     var useCSS = gPerformancingUtil.prefs.getBoolPref("settings.usecss");
  69.     midas.contentDocument.execCommand("styleWithCSS", false, useCSS);
  70.     
  71.     //midas.contentWindow.document.body.innerHTML = "<p> </p>";
  72.     midas.contentWindow.document.body.innerHTML = "<br/>";
  73.     midas.contentWindow.focus()
  74. }
  75.  
  76. performancingMidas.doSomeEditCommand = function(aName, aArg) {
  77.     var bfEditorTabSelected = document.getElementById("performancing-editor-tabbox").selectedIndex;
  78.     if(bfEditorTabSelected == 1){
  79.         performancingTextEdit.insertCode(aName, null, null);
  80.     }else{
  81.         this.doRichEditCommand(aName, aArg);
  82.     }
  83.  
  84. performancingMidas.doRichEditCommand = function(aName, aArg) {
  85.     var midas = document.getElementById("performancing-message");
  86.     // substitute undefined command identifiers
  87.     midas.contentDocument.execCommand(aName,false, aArg);
  88.     midas.contentWindow.focus()
  89.  
  90. performancingMidas.addLink = function(){
  91.         var midas = document.getElementById("performancing-message");
  92.         var bfEditorTabSelected = document.getElementById("performancing-editor-tabbox").selectedIndex;
  93.         if(bfEditorTabSelected == '1'){
  94.             performancingTextEdit.insertCode('url', null, null);
  95.         }else{
  96.             var localeString = performancingUI.getLocaleString('midaseenterurl', []);
  97.             var myUrl = prompt(localeString, "http://");
  98.             if (myUrl != null && myUrl != "http://") {
  99.                 midas.contentDocument.execCommand("createLink", false, myUrl);
  100.             }
  101.         }
  102. }
  103.  
  104. performancingMidas.blockquote = function(){
  105.   performancingMidas.doApplyTag("blockquote");
  106. }
  107.  
  108.  
  109. performancingMidas.strong = function(){
  110.   performancingMidas.doApplyTag("strong");
  111. }
  112.  
  113. performancingMidas.em = function(){
  114.   performancingMidas.doApplyTag("em");
  115. }
  116.  
  117. performancingMidas.doApplyTag = function(sTagName) {
  118.   var bfEditorTabSelected, midas, oRange, sRange, newNode;
  119.   bfEditorTabSelected = document.getElementById("performancing-editor-tabbox").selectedIndex;
  120.   midas = document.getElementById("performancing-message");
  121.   oRange = midas.contentWindow.getSelection().getRangeAt(0);  // this will exist only in the rich text editor
  122.     if(bfEditorTabSelected == '1'){
  123.       performancingTextEdit.insertCode(sTagName, null, null);
  124.     } else if(performancingMidas.isTagPresent(sTagName, oRange) && !oRange.collapsed) {
  125.       performancingMidas.removeTag(oRange);
  126.     } else {
  127.       sRange = oRange.toString();
  128.       oRange.deleteContents();
  129.       newNode = midas.contentDocument.createElement(sTagName);
  130.       newNode.appendChild(midas.contentDocument.createTextNode(sRange));
  131.       oRange.insertNode(newNode);
  132.  
  133.       // TODO: set focus back AFTER the range
  134.       sRange = null;
  135.       newNode = null;
  136.     }
  137.  
  138.   bfEditorTabSelected = null;
  139.   midas = null;
  140.   if(oRange) oRange.detach();
  141. }
  142.  
  143. performancingMidas.isTagPresent = function(sTagName, oRange) {
  144.   sTagName = sTagName.toUpperCase();
  145.   if(oRange.startContainer.parentNode.nodeName == sTagName || oRange.endContainer.parentNode.nodeName == sTagName)
  146.     return true;
  147.   else
  148.     return false;
  149. }
  150.  
  151. performancingMidas.removeTag = function(oRange) {
  152.   var sRange = oRange.toString();
  153.   var midas = document.getElementById("performancing-message");
  154.   oRange.deleteContents();
  155.   oRange.insertNode(midas.contentDocument.createTextNode(sRange));
  156.   sRange = null;
  157.   return false;
  158. }
  159.  
  160. //Not used blockquote
  161. performancingMidas.bold = function(){
  162.         var midas = document.getElementById("performancing-message");
  163.         var bfEditorTabSelected = document.getElementById("performancing-editor-tabbox").selectedIndex;
  164.         if(bfEditorTabSelected == '1'){
  165.             performancingTextEdit.insertCode('url', null, null);
  166.         }else{
  167.             var strong = midas.contentWindow.document.createElement("strong");
  168.             var sel = midas.contentWindow.getSelection();
  169.             var range = sel.getRangeAt(0);
  170.             var beforeNode = range.extractContents();
  171.             //var beforeNode = document.createTextNode(range);
  172.             strong.appendChild(beforeNode);//Range is not an object! Damn it!
  173.             this.insertNodeAtSelection(midas.contentWindow, strong, null);
  174.             //alert("bold!");
  175.         }
  176. }
  177.  
  178.  
  179. performancingMidas.addImage = function() {
  180.         var midas = document.getElementById("performancing-message");
  181.         
  182.         var bfEditorTabSelected = document.getElementById("performancing-editor-tabbox").selectedIndex;
  183.         if(bfEditorTabSelected == '1'){
  184.             performancingTextEdit.insertCode('img', null, null);
  185.         }else{
  186.             /*
  187.             var imagetag = prompt("Please enter a Image URL:", "http://");
  188.             //alert('imagetag:' + imagetag+':dude');
  189.             if (imagetag != "http://" && imagetag != null) {
  190.                 var img = midas.contentWindow.document.createElement("img");
  191.                 img.setAttribute("src",imagetag);
  192.                 this.insertNodeAtSelection(midas.contentWindow, img);
  193.                 this.placeCaret(midas, img.nextSibling);
  194.             }
  195.             */
  196.             var theImageToInsert = performancingUI.openImageUpload();
  197.             if(theImageToInsert != false){
  198.                 var img = midas.contentWindow.document.createElement("img");
  199.                     img.setAttribute("src",theImageToInsert);
  200.                     this.insertNodeAtSelection(midas.contentWindow, img);
  201.                     this.placeCaret(midas, img.nextSibling);
  202.             }
  203.         }
  204. }
  205.  
  206. /*
  207.     For Testing only
  208. */
  209. performancingMidas.addImage2 = function() {
  210.         var midas = document.getElementById("performancing-message");
  211.         
  212.         var bfEditorTabSelected = document.getElementById("performancing-editor-tabbox").selectedIndex;
  213.         if(bfEditorTabSelected == '1'){
  214.             performancingTextEdit.insertCode('img', null, null);
  215.         }else{
  216.             /*
  217.             var imagetag = prompt("Please enter a Image URL:", "http://");
  218.             //alert('imagetag:' + imagetag+':dude');
  219.             if (imagetag != "http://" && imagetag != null) {
  220.                 var img = midas.contentWindow.document.createElement("img");
  221.                 img.setAttribute("src",imagetag);
  222.                 this.insertNodeAtSelection(midas.contentWindow, img);
  223.                 this.placeCaret(midas, img.nextSibling);
  224.             }
  225.             */
  226.             var theImageToInsert = performancingUI.openImageUpload();
  227.             if(theImageToInsert != false){
  228.                 var img = midas.contentWindow.document.createElement("img");
  229.                     img.setAttribute("src",theImageToInsert);
  230.                     this.insertNodeAtSelection(midas.contentWindow, img);
  231.                     this.placeCaret(midas, img.nextSibling);
  232.             }
  233.         }
  234. }
  235.  
  236. performancingMidas.insertNodeAtSelection = function(win, insertNode, type) {//type = before, after null
  237.     // get current selection
  238.     var sel = win.getSelection();
  239.     //alert("sel: "+ sel + " sel.getRangeAt(0): " + sel.getRangeAt(0));
  240.  
  241.     // get the first range of the selection
  242.     // (there's almost always only one range)
  243.     var range = sel.getRangeAt(0);
  244.  
  245.     // deselect everything
  246.     sel.removeAllRanges();
  247.  
  248.     // remove content of current selection from document
  249.     //var clone = null;
  250.     if(type == null){
  251.         range.deleteContents();
  252.     }
  253.  
  254.     // get location of current selection
  255.     var container = range.startContainer;
  256.     var pos = range.startOffset;
  257.     if(type == "after"){
  258.         container = range.endContainer;
  259.         pos = range.endOffset;
  260.     }
  261.  
  262.     // make a new range for the new selection
  263.     range=document.createRange();
  264.  
  265.     if (container.nodeType==3 && insertNode.nodeType==3) {
  266.         // if we insert text in a textnode, do optimized insertion
  267.         container.insertData(pos, insertNode.nodeValue);
  268.  
  269.         // put cursor after inserted text
  270.         range.setEnd(container, pos+insertNode.length);
  271.         range.setStart(container, pos+insertNode.length);
  272.     } else {
  273.         var afterNode;
  274.         if (container.nodeType==3) {
  275.             // when inserting into a textnode
  276.             // we create 2 new textnodes
  277.             // and put the insertNode in between
  278.             var textNode = container;
  279.             container = textNode.parentNode;
  280.             var text = textNode.nodeValue;
  281.  
  282.             // text before the split
  283.             var textBefore = text.substr(0,pos);
  284.             // text after the split
  285.             var textAfter = text.substr(pos);
  286.  
  287.             var beforeNode = document.createTextNode(textBefore);
  288.             var afterNode = document.createTextNode(textAfter);
  289.  
  290.             // insert the 3 new nodes before the old one
  291.             container.insertBefore(afterNode, textNode);
  292.             container.insertBefore(insertNode, afterNode);
  293.             container.insertBefore(beforeNode, insertNode);
  294.  
  295.             // remove the old node
  296.             container.removeChild(textNode);
  297.         } else {
  298.             // else simply insert the node
  299.             afterNode = container.childNodes[pos];
  300.             container.insertBefore(insertNode, afterNode);
  301.         }
  302.         range.setEnd(afterNode, 0);
  303.         range.setStart(afterNode, 0);
  304.     }
  305.  
  306.     sel.addRange(range);
  307. }
  308.  
  309.  
  310.  
  311. //Places the caret inside and at the beginning of the node specified
  312. performancingMidas.placeCaret = function(win, node) {
  313.     var sel = win.contentWindow.getSelection();
  314.     var range = sel.getRangeAt(0);
  315.     sel.removeAllRanges();
  316.     range=document.createRange();
  317.     range.setEnd(node, 0);
  318.     range.setStart(node, 0);
  319.     sel.addRange(range);
  320.     win.contentWindow.focus();
  321. }
  322.  
  323. //
  324. performancingMidas.placeCaretLite = function(win) {
  325.     var sel = win.contentWindow.getSelection();
  326.     var range = sel.getRangeAt(0);
  327.     sel.removeAllRanges();
  328.     range=document.createRange();
  329.     sel.addRange(range);
  330.     win.contentWindow.focus();
  331. }
  332.  
  333. //For generic XHTML compliance
  334. performancingMidas.getXHTML = function(text) {
  335.     //Fix closing tags on single elements
  336.     text = text.replace(/<(br|hr|img)([^>]*)([^\/])>/gi, "<$1$2$3 />");
  337.     //Remove extra space in case it was formatted as <br > - not a big deal
  338.     text = text.replace(/<(br|hr|img)([^>]*)  \/>/gi, "<$1$2 />");
  339.     //Add space if it is <br style=""/> at this point
  340.     text = text.replace(/<(br|hr|img)([^>]*)"\/>/gi, "<$1$2\" />");
  341.     //Make sure br is closed
  342.     text = text.replace(/<br>/gi, "<br />");
  343.     return text;
  344. }
  345.  
  346.  
  347. // Source -> Normal (note: DOM is NOT compliant at this point)
  348. // 1) The value of the Source tab's textbox is stored in a variable and modified using RegExp's to make it DOM compliant and any other formating that should be done at this time.
  349. // 2) The innerHTML of the Preview tab's frame is replaced with the value from the variable.
  350. // 3) Any DOM modifications that are needed for the Normal tab are made to the Preview tab's frame.
  351. // 4) The innerHTML of the Normal tab's editor is replaced with the innerHTML from the Preview tab's frame.
  352. // (note: Preview handles adding the subject and just about everything else is taken from the Normal tab
  353. performancingMidas.syncNormalTab = function() {
  354.     //***Begin RegExp phase***
  355.     var text = document.getElementById("performancing-message-source").value;
  356.  
  357.     if (this.dontautoformat.toString() != "true") {
  358.         //When leaving source view we only use the DS specific classes for br's when "Don't auto-format" is NOT checked.
  359.         text = text.replace(/(\r)?\n/gi, "<br/>");
  360.         text = text.replace(/<br( \/)?>/gi, "<br/>");
  361.     }
  362.  
  363.  
  364.     //For generic XHTML compliance whether the user wants them or not?
  365.     text = this.getXHTML(text);
  366.  
  367.     //***End RegExp phase***
  368.     try{
  369.         //***Begin DOM phase***
  370.         var winNormal = document.getElementById("performancing-message");
  371.         var winPreview = document.getElementById("performancing-preview-display");
  372.         winPreview.contentWindow.document.body.innerHTML = text;
  373.         var elements = winPreview.contentDocument.getElementsByTagName("*");
  374.     
  375.         //this.cleanup();
  376.         //***End DOM phase***
  377.     
  378.         text = winPreview.contentWindow.document.body.innerHTML;
  379.     
  380.         winNormal.contentWindow.document.body.innerHTML = text;
  381.     }catch(e){
  382.         //Foo
  383.     }
  384. }
  385.  
  386. // Normal -> Source (note: DOM is compliant at this point)
  387. // 1) The innerHTML in the Preview tab's frame is replaced with the innerHTML from the Normal tab's editor.
  388. // 2) Any DOM modifications that are needed for the Source tab are made to the Preview tab's frame.
  389. // 3) The innerHTML of the Preview tab's frame is then stored in a variable and further modified using RegExp's.
  390. // 4) The value of the Source tab's textbox is then replaced with the contents of the variable.
  391. performancingMidas.syncSourceTab = function() {
  392.  
  393.     //***Begin DOM phase***
  394.     var winNormal = document.getElementById("performancing-message");
  395.     var winPreview = document.getElementById("performancing-preview-display");
  396.     try{
  397.         winPreview.contentWindow.document.body.innerHTML =    winNormal.contentWindow.document.body.innerHTML;
  398.         //this.cleanup();
  399.     
  400.         var text = winPreview.contentWindow.document.body.innerHTML;
  401.         //***End DOM phase***
  402.         //***Begin RegExp phase***
  403.         //replace 0 or 1 newline followed by a br followed by 0 or 1 newline with a formatted br
  404.         text = text.replace(/<br([^>]*)>[\r\f]?\n/gi, "<br$1 />");
  405.         text = text.replace(/[\r\f]\n? ?/gi, " ");
  406.         text = text.replace(/\n ?/gi, " ");
  407.     
  408.         //remove all br's at the end of the string. The editor usually adds at least one.
  409.         text = text.replace(/<br( \/)?>$/gi, "");
  410.     
  411.         if (this.dontautoformat.toString() != "true") {
  412.             //Replace any new br's entered in the message tab with newlines
  413.             text = text.replace(/<br( \/)?>/gi,"\n");
  414.         }
  415.     
  416.     
  417.         //For generic XHTML compliance whether the user wants them or not?
  418.         text = this.getXHTML(text);
  419.     
  420.         //***End RegExp phase***
  421.     
  422.         document.getElementById("performancing-message-source").value = text;
  423.     }catch(e){}
  424. }
  425.  
  426. performancingMidas.viewTab = function(tabname) {
  427.     // To move between textbox and iframe. The 'this.sourceview' thing is to stop it blanking the post if you click
  428.     // the same tab that you're already on. (ie. if you're in source view and click the 'source' tab)
  429.     var theClickedID = tabname.id;
  430.     
  431.     var tabbox = document.getElementById("performancing-editor-tabbox");
  432.     
  433.     var theTabList = ["tab-normal-edit","tab-source-edit","tab-preview-edit"];
  434.     for(var i=0; i < theTabList.length; i++){
  435.         if(theTabList[i] != theClickedID){
  436.             document.getElementById(theTabList[i]).setAttribute("isselected","false");
  437.         }
  438.     }
  439.     
  440.     switch(theClickedID){
  441.         case "tab-normal-edit":
  442.             performancingUI.theSpellcheck = performancingUI.richSpellCheck;
  443.             tabbox.selectedIndex = 0;
  444.             tabname.setAttribute("isselected","true");
  445.             gPerformancingUtil.prefs.setCharPref('display.state.lastviewopened', theClickedID);
  446.             break;
  447.             
  448.         case "tab-source-edit":
  449.             performancingUI.theSpellcheck = performancingUI.sourceSpellCheck;
  450.             tabbox.selectedIndex = 1;
  451.             tabname.setAttribute("isselected","true");
  452.             gPerformancingUtil.prefs.setCharPref('display.state.lastviewopened', theClickedID);
  453.             break;
  454.             
  455.         case "tab-preview-edit":
  456.             tabbox.selectedIndex = 2;
  457.             tabname.setAttribute("isselected","true");
  458.             break;
  459.             
  460.     }
  461.     
  462.     var selectedTab = tabbox.selectedIndex;
  463.     
  464.     
  465.     //if (selectedTab == 0 && this.sourceview) {
  466.     if (selectedTab == 0 && gPerFormancingSelectedTab != 0) {
  467.         this.syncNormalTab();
  468. //This is still buggy
  469. //        placeCaret(document.getElementById("message"), document.getElementById("message").contentWindow.document.body.lastChild);
  470.         document.getElementById("performancing-message").contentWindow.focus();
  471.         this.sourceview = false;
  472.         gPerFormancingSelectedTab = 0;
  473.     //} else if (selectedTab == 1 && !this.sourceview) {
  474.     } else if (selectedTab == 1     && gPerFormancingSelectedTab != 1) {
  475.         this.syncSourceTab();
  476.         document.getElementById("performancing-message-source").setSelectionRange(document.getElementById("performancing-message-source").textLength, document.getElementById("performancing-message-source").textLength);
  477.         document.getElementById("performancing-message-source").focus();
  478.         this.sourceview = true;
  479.         gPerFormancingSelectedTab = 1;
  480.     } else if (selectedTab == 2     && gPerFormancingSelectedTab != 2) {
  481.         if (this.sourceview == true) {
  482.             this.syncNormalTab();
  483.         } else {
  484.             this.syncSourceTab();
  485.         }
  486.         this.previewPost();
  487.         gPerFormancingSelectedTab = 2;
  488.     }else{
  489.         //alert('Error, no tab selected');
  490.     }
  491. }
  492.  
  493. // Say welcome back to the Preview function
  494. performancingMidas.previewPost = function() {
  495.     var winNormal = document.getElementById("performancing-message");
  496.     var winPreview = document.getElementById("performancing-preview-display");
  497.     //Always use Normal tab for this since it is almost completely formatted for this already... yes, it is read twice when coming from the source tab
  498.     var copiedToPreview = winNormal.contentWindow.document.body.innerHTML + "\n"; //to avoid removing the last line which might not end in a newline?
  499.     var subjectPreview = document.getElementById("performancing-editor-subject").value;
  500.     
  501.     if (subjectPreview) {
  502.         copiedToPreview = "<strong>"+ subjectPreview + ":</strong><br><br>" + copiedToPreview;
  503.     }
  504.     winPreview.contentDocument.body.innerHTML = copiedToPreview;
  505. }
  506.  
  507.  
  508. performancingMidas.toggleSidebar = function(forceOpen) {
  509.     var performancingSidebarElement = document.getElementById("performancing-sidebar-outer");
  510.     var performancingSidebarButton = document.getElementById("performancing-sidebarbutton");
  511.     
  512.     var isCollapsed = performancingSidebarElement.getAttribute('collapsed');
  513.     if(isCollapsed == 'true' || forceOpen){
  514.         performancingSidebarElement.setAttribute('collapsed', false);
  515.         performancingSidebarButton.setAttribute('state', 'open');
  516.         var localeString = performancingUI.getLocaleString('closesidebar', []);
  517.         performancingSidebarButton.setAttribute('tooltiptext', localeString);
  518.     }else{
  519.         performancingSidebarElement.setAttribute('collapsed', true);
  520.         performancingSidebarButton.setAttribute('state', 'closed');
  521.         var localeString = performancingUI.getLocaleString('opensidebar', []);
  522.         performancingSidebarButton.setAttribute('tooltiptext', localeString);
  523.     }
  524. }
  525.  
  526. //Handle preview link clicks
  527. performancingMidas.previewClick = function(event) {
  528.     var linkString;
  529.     var isLink = false;
  530.     try{
  531.       //The parentNode is just incase we have an image or other object wrapped around a mailto link.
  532.       linkString = event.originalTarget.parentNode.getAttribute('href').toString();
  533.       isLink = true;
  534.     }catch (e) {
  535.       linkString = "Null";
  536.     }
  537.     
  538.     try{
  539.       linkString = event.originalTarget.getAttribute('href').toString();
  540.       isLink = true;
  541.     }catch (e) {
  542.       //Foobar
  543.     }
  544.     if(isLink == true){
  545.         event.preventDefault();
  546.         performancingUI.openInTab(linkString);
  547.     }
  548. }
  549.  
  550. //Used for Tag insertion in Source Editor
  551. performancingTextEdit.insertCode = function(myCommand, codeType, mycustomstring) {
  552.     var clip = Components.classes["@mozilla.org/widget/clipboard;1"].createInstance(Components.interfaces.nsIClipboard);
  553.     if (!clip){
  554.       return false;
  555.     }
  556.     
  557.     
  558.     var trans = Components.classes["@mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable);
  559.     if (!trans){
  560.       return false;
  561.     }
  562.     
  563.       trans.addDataFlavor("text/unicode");
  564.       clip.getData(trans,clip.kGlobalClipboard);
  565.  
  566.     
  567.     var str = null;
  568.     var str_clipboard=new Object();
  569.     var strLength=new Object();
  570.     try{
  571.       trans.getTransferData("text/unicode",str_clipboard,strLength);
  572.   
  573.   
  574.       if (str_clipboard){
  575.         str_clipboard=str_clipboard.value.QueryInterface(Components.interfaces.nsISupportsString);
  576.       }
  577.       if (str_clipboard){
  578.         pastetext=str_clipboard.data.substring(0,strLength.value / 2);
  579.       }
  580.     }catch (e){
  581.       //alert("No text in the clipboard, please copy something first.");
  582.     }
  583.     
  584.     //var theBox = document.commandDispatcher.focusedElement;
  585.     var theBox = document.getElementById("performancing-message-source");
  586.     var oPosition = theBox.scrollTop;
  587.     var oHeight = theBox.scrollHeight;
  588.     
  589.     
  590.     //Get Selected text (if selected)
  591.       var startPos = theBox.selectionStart;
  592.       var endPos = theBox.selectionEnd;
  593.       str = theBox.value.substring(startPos, endPos);
  594.       //alert("Str: " +str);
  595.  
  596.     //if(codeType == "bbcode"){
  597.     if( gPerformancingUtil.prefs.getBoolPref('settings.usecss') ){
  598.         performancingTextEdit.insertAtCursorSetup(myCommand, str_clipboard, str, theBox, mycustomstring);
  599.     }else{
  600.         performancingTextEdit.insertHTMLAtCursorSetup(myCommand, str_clipboard, str, theBox, mycustomstring);
  601.     }
  602.     
  603.     var nHeight = theBox.scrollHeight - oHeight;
  604.     theBox.scrollTop = oPosition + nHeight;
  605.     
  606.     return true;
  607. }
  608.  
  609. //CSS!
  610. performancingTextEdit.insertAtCursorSetup = function(myCommand, str_clipboard, str, theBox) {
  611.     var bold = "<strong>";
  612.     var boldend="<\/strong>";
  613.     var color = "<span style=\"color: ";
  614.     var color2 = "\">";
  615.     var colorend = "<\/span>";
  616.     var colorend2 = "</span>";
  617.     var select_flag = (str != "");
  618.     switch (myCommand){
  619.         
  620.       case "img":
  621.         /*
  622.         var imagetag = prompt("Please enter a Image URL:", "http://");
  623.         if (imagetag != "http://" && imagetag != null) {
  624.             performancingTextEdit.insertAtCursor("<img src=\"" + imagetag + "\"/>");
  625.         }
  626.         */
  627.         var theImageToInsert = performancingUI.openImageUpload();
  628.         if(theImageToInsert != false){
  629.             performancingTextEdit.insertAtCursor("<img src=\"" + theImageToInsert + "\"/>");
  630.         }
  631.         
  632.         break;
  633.         
  634.       case "url2":
  635.         performancingTextEdit.insertAtCursorEx("<a href=\"\">" + str,"</a>",select_flag);
  636.         break;
  637.         
  638.       case "url":
  639.         var localeString = performancingUI.getLocaleString('midaseenterurl', []);
  640.         var myUrl = prompt(localeString, "http://");
  641.         if (myUrl != null && myUrl != "http://") {
  642.             performancingTextEdit.insertAtCursorEx("<a href=\"" + myUrl + "\">" + str,"</a>",select_flag);
  643.         }
  644.         break;
  645.         
  646.       case "bold":
  647.         performancingTextEdit.insertAtCursorEx("<span style=\"font-weight: bold;\">" + str,"</span>",select_flag);
  648.         break;
  649.         
  650.       case "italic":
  651.         performancingTextEdit.insertAtCursorEx("<span style=\"font-style: italic;\">" + str,"</span>",select_flag);
  652.         break;
  653.         
  654.       case "underline":
  655.         performancingTextEdit.insertAtCursorEx("<span style=\"text-decoration: underline;\">" + str,"</span>",select_flag);
  656.         break;
  657.       
  658.       case "strikethrough":
  659.         performancingTextEdit.insertAtCursorEx("<span style=\"text-decoration: line-through;\">" + str,"</span>",select_flag);
  660.         break;
  661.         
  662.       case "strong":
  663.         performancingTextEdit.insertAtCursorEx("<strong>" + str,"</strong>",select_flag);
  664.         break;
  665.         
  666.       case "em":
  667.         performancingTextEdit.insertAtCursorEx("<em>" + str,"</em>",select_flag);
  668.         break;
  669.         
  670.       case "blockquote":
  671.         performancingTextEdit.insertAtCursorEx("<blockquote>" + str,"</blockquote>",select_flag);
  672.         break;
  673.         
  674.       case "increasefontsize":
  675.         performancingTextEdit.insertAtCursorEx("<big>" + str,"</big>",select_flag);
  676.         break;
  677.         
  678.       case "decreasefontsize":
  679.         performancingTextEdit.insertAtCursorEx("<small>" + str,"</small>",select_flag);
  680.         break;
  681.         
  682.       case "justifyleft":
  683.         performancingTextEdit.insertAtCursorEx("<div style=\"text-align: left;\">" + str,"</div>",select_flag);
  684.         break;
  685.         
  686.       case "justifycenter":
  687.         performancingTextEdit.insertAtCursorEx("<div style=\"text-align: center;\">" + str,"</div>",select_flag);
  688.         break;
  689.         
  690.       case "justifyright":
  691.         performancingTextEdit.insertAtCursorEx("<div style=\"text-align: right;\">" + str,"</div>",select_flag);
  692.         break;
  693.         
  694.       case "justifyfull":
  695.         performancingTextEdit.insertAtCursorEx("<div style=\"text-align: justify;\">" + str,"</div>",select_flag);
  696.         break;
  697.         
  698.       case "insertorderedlist":
  699.         var eachLineArray = str.split("\n");
  700.         var newLineString = "";
  701.         for(i=0; i< eachLineArray.length; i++){
  702.           if(newLineString == ""){
  703.             newLineString = newLineString +"<li>" + eachLineArray[i] + "</li>";
  704.           }else{
  705.             newLineString = newLineString +"<li>" + eachLineArray[i] + "</li>";
  706.           }
  707.         }
  708.         performancingTextEdit.insertAtCursor("<ol>"     + newLineString + "</ol>");
  709.         break;
  710.         
  711.       case "insertunorderedlist":
  712.         var eachLineArray = str.split("\n");
  713.         var newLineString = "";
  714.         for(i=0; i< eachLineArray.length; i++){
  715.           if(newLineString == ""){
  716.             newLineString = newLineString +"<li>" + eachLineArray[i] + "</li>";
  717.           }else{
  718.             newLineString = newLineString +"<li>" + eachLineArray[i] + "</li>";
  719.           }
  720.         }
  721.         performancingTextEdit.insertAtCursor("<ul>"     + newLineString + "</ul>");
  722.         break;
  723.         
  724.       case "forecolor2":
  725.         
  726.         var re = [];
  727.         var mycolor = ["#000"];
  728.         var localeString = performancingUI.getLocaleString('midascolor', []);
  729.         window.openDialog("chrome://performancing/content/color.xul", localeString,"chrome,modal,centerscreen",re,mycolor);
  730.         if(re[0]){
  731.           performancingTextEdit.insertAtCursorEx("<span style=\"color: " + mycolor[0] + "\">" + str,"</span>",select_flag);
  732.           //myColor = mycolor[0];
  733.         }
  734.         break;
  735.         
  736.       case "forecolor":
  737.         if(gPerFormancingColorPick == false){
  738.             gPerFormancingColorPick = true;
  739.             var mycolor = document.getElementById("performance-color-picker").color;
  740.             document.getElementById("performancing-message-source").focus();
  741.             performancingTextEdit.insertAtCursorEx("<span style=\"color: " + mycolor + "\">" + str,"</span>",select_flag);
  742.         }
  743.         window.setTimeout(performancingTextEdit.setColorPicker, 2000, true);
  744.         break;
  745.  
  746.         
  747.       default :
  748.         var localeString = performancingUI.getLocaleString('notimplementederror', []);
  749.         alert(localeString);
  750.     }
  751. }
  752.  
  753. //HTML!
  754. performancingTextEdit.insertHTMLAtCursorSetup = function(myCommand, str_clipboard, str, theBox) {
  755.     var bold = "<strong>";
  756.     var boldend="<\/strong>";
  757.     var color = "<font color=\"";
  758.     var color2 = "\">";
  759.     var colorend = "<\/font>";
  760.     var colorend2 = "</span>";
  761.     var select_flag = (str != "");
  762.     switch (myCommand){
  763.         
  764.       case "img":
  765.       /*
  766.         var imagetag = prompt("Please enter a Image URL:", "http://");
  767.         if (imagetag != "http://" && imagetag != null) {
  768.             performancingTextEdit.insertAtCursor("<img src=\"" + imagetag + "\"/>");
  769.         }
  770.         */
  771.         var theImageToInsert = performancingUI.openImageUpload();
  772.         if(theImageToInsert != false){
  773.             performancingTextEdit.insertAtCursor("<img src=\"" + theImageToInsert + "\"/>");
  774.         }
  775.         break;
  776.         
  777.       case "url2":
  778.         performancingTextEdit.insertAtCursorEx("<a href=\"\">" + str,"</a>",select_flag);
  779.         break;
  780.         
  781.       case "url":
  782.         var localeString = performancingUI.getLocaleString('midaseenterurl', []);
  783.         var myUrl = prompt(localeString, "http://");
  784.         if (myUrl != null && myUrl != "http://") {
  785.             performancingTextEdit.insertAtCursorEx("<a href=\"" + myUrl + "\">" + str,"</a>",select_flag);
  786.         }
  787.         break;
  788.         
  789.       case "bold":
  790.         performancingTextEdit.insertAtCursorEx("<b>" + str,"</b>",select_flag);
  791.         break;
  792.         
  793.       case "italic":
  794.         performancingTextEdit.insertAtCursorEx("<i>" + str,"</i>",select_flag);
  795.         break;
  796.         
  797.       case "underline":
  798.         performancingTextEdit.insertAtCursorEx("<u>" + str,"</u>",select_flag);
  799.         break;
  800.       
  801.       case "strikethrough":
  802.         performancingTextEdit.insertAtCursorEx("<strike>" + str,"</strike>",select_flag);
  803.         break;
  804.         
  805.       case "blockquote":
  806.         performancingTextEdit.insertAtCursorEx("<blockquote>" + str,"</blockquote>",select_flag);
  807.         break;
  808.         
  809.       case "strong":
  810.         performancingTextEdit.insertAtCursorEx("<strong>" + str,"</strong>",select_flag);
  811.         break;
  812.         
  813.       case "em":
  814.         performancingTextEdit.insertAtCursorEx("<em>" + str,"</em>",select_flag);
  815.         break;
  816.         
  817.       case "increasefontsize":
  818.         performancingTextEdit.insertAtCursorEx("<big>" + str,"</big>",select_flag);
  819.         break;
  820.         
  821.       case "decreasefontsize":
  822.         performancingTextEdit.insertAtCursorEx("<small>" + str,"</small>",select_flag);
  823.         break;
  824.         
  825.       case "justifyleft":
  826.         performancingTextEdit.insertAtCursorEx("<div align=\"left\">" + str,"</div>",select_flag);
  827.         break;
  828.         
  829.       case "justifycenter"://<div align="left">
  830.         performancingTextEdit.insertAtCursorEx("<div align=\"center\">" + str,"</div>",select_flag);
  831.         break;
  832.         
  833.       case "justifyright":
  834.         performancingTextEdit.insertAtCursorEx("<div align=\"right\">" + str,"</div>",select_flag);
  835.         break;
  836.         
  837.       case "justifyfull":
  838.         performancingTextEdit.insertAtCursorEx("<div align=\"justify\">" + str,"</div>",select_flag);
  839.         break;
  840.         
  841.       case "insertorderedlist":
  842.         var eachLineArray = str.split("\n");
  843.         var newLineString = "";
  844.         for(i=0; i< eachLineArray.length; i++){
  845.           if(newLineString == ""){
  846.             newLineString = newLineString +"<li>" + eachLineArray[i] + "</li>";
  847.           }else{
  848.             newLineString = newLineString +"<li>" + eachLineArray[i] + "</li>";
  849.           }
  850.         }
  851.         performancingTextEdit.insertAtCursor("<ol>"     + newLineString + "</ol>");
  852.         break;
  853.         
  854.       case "insertunorderedlist":
  855.         var eachLineArray = str.split("\n");
  856.         var newLineString = "";
  857.         for(i=0; i< eachLineArray.length; i++){
  858.           if(newLineString == ""){
  859.             newLineString = newLineString +"<li>" + eachLineArray[i] + "</li>";
  860.           }else{
  861.             newLineString = newLineString +"<li>" + eachLineArray[i] + "</li>";
  862.           }
  863.         }
  864.         performancingTextEdit.insertAtCursor("<ul>"     + newLineString + "</ul>");
  865.         break;
  866.         
  867.       case "forecolor2":
  868.         
  869.         var re = [];
  870.         var mycolor = ["#000"];
  871.         window.openDialog("chrome://performancing/content/color.xul", "PerFormancing Color","chrome,modal,centerscreen",re,mycolor);
  872.         if(re[0]){
  873.           performancingTextEdit.insertAtCursorEx("<font color=\"" + mycolor[0] + "\">" + str,"</font>",select_flag);
  874.           //myColor = mycolor[0];
  875.         }
  876.         break;
  877.         
  878.       case "forecolor":
  879.         if(gPerFormancingColorPick == false){
  880.             gPerFormancingColorPick = true;
  881.             var mycolor = document.getElementById("performance-color-picker").color;
  882.             document.getElementById("performancing-message-source").focus();
  883.             performancingTextEdit.insertAtCursorEx("<font color=\"" + mycolor + "\">" + str,"</font>",select_flag);
  884.         }
  885.         window.setTimeout(performancingTextEdit.setColorPicker, 2000, true);
  886.         break;
  887.  
  888.         
  889.       default : 
  890.         var localeString = performancingUI.getLocaleString('notimplementederror', []);
  891.         alert(localeString);
  892.     }
  893. }
  894.  
  895. performancingTextEdit.setColorPicker = function() {
  896.     gPerFormancingColorPick = false;
  897. }
  898.  
  899. performancingTextEdit.insertAtCursor = function( aText) {
  900.     try {
  901.       var command = "cmd_insertText";
  902.       var controller = document.commandDispatcher.getControllerForCommand(command);
  903.       if (controller && controller.isCommandEnabled(command)) {
  904.         controller = controller.QueryInterface(Components.interfaces.nsICommandController);
  905.         var params = Components.classes["@mozilla.org/embedcomp/command-params;1"];
  906.         params = params.createInstance(Components.interfaces.nsICommandParams);
  907.         params.setStringValue("state_data", aText);
  908.         controller.doCommandWithParams(command, params);
  909.       }
  910.     }catch (e) {
  911.       //alert("Can't do cmd_insertText! ");
  912.       //dump(e+"\n")
  913.     }
  914. }
  915.  
  916. performancingTextEdit.insertAtCursorEx = function(aText, bText, selection) {
  917.       this.insertAtCursor(aText+bText);
  918.       if ( selection ) {
  919.           //this.reselectCursor(aText+bText);
  920.       } else {
  921.           this.backupCursor(bText);
  922.       }     
  923. }
  924.  
  925. //Original Function by Asquella, modified by Patrick Wildenborg
  926. performancingTextEdit.backupCursor = function(aText) {
  927.     try {
  928.       var command = "cmd_charPrevious";
  929.       var controller = document.commandDispatcher.getControllerForCommand(command);
  930.       if (controller && controller.isCommandEnabled(command)) {
  931.         controller = controller.QueryInterface(Components.interfaces.nsICommandController);
  932.         var params = Components.classes["@mozilla.org/embedcomp/command-params;1"];
  933.         params = params.createInstance(Components.interfaces.nsICommandParams);
  934.         var len = aText.length
  935.         while( len-- > 0 ) {
  936.             controller.doCommand(command, params);
  937.         }
  938.       }
  939.     }catch (e) {
  940.       //dump("Can't do cmd_insertText! ");
  941.       //dump(e+"\n")
  942.     }
  943. }
  944.  
  945. performancingTextEdit.moveCursorForward = function(aText) {
  946.     try {
  947.       var command = "cmd_charNext";
  948.       var controller = document.commandDispatcher.getControllerForCommand(command);
  949.       if (controller && controller.isCommandEnabled(command)) {
  950.         controller = controller.QueryInterface(Components.interfaces.nsICommandController);
  951.         var params = Components.classes["@mozilla.org/embedcomp/command-params;1"];
  952.         params = params.createInstance(Components.interfaces.nsICommandParams);
  953.         var len = aText.length
  954.         while( len-- > 0 ) {
  955.             controller.doCommand(command, params);
  956.         }
  957.       }
  958.     }catch (e) {
  959.       //dump("Can't do cmd_insertText! ");
  960.       //dump(e+"\n")
  961.     }
  962. }
  963.  
  964. //Original Function by Asquella, modified by Patrick Wildenborg
  965. //Bug: If there is a line wrap in the text box, we are 1 char off on the selection.
  966. // Not sure how to check that boundery condition.
  967. performancingTextEdit.reselectCursor = function(aText) {
  968.     try {
  969.       var command = "cmd_selectCharPrevious";
  970.       var controller = document.commandDispatcher.getControllerForCommand(command);
  971.       if (controller && controller.isCommandEnabled(command)) {
  972.         controller = controller.QueryInterface(Components.interfaces.nsICommandController);
  973.         var params = Components.classes["@mozilla.org/embedcomp/command-params;1"];
  974.         params = params.createInstance(Components.interfaces.nsICommandParams);
  975.         var len = aText.length
  976.         while( len-- > 0 ) {
  977.             controller.doCommand(command, params);
  978.         }
  979.       }
  980.     }catch (e) {
  981.       //dump("Can't do cmd_insertText! ");
  982.       //dump(e+"\n")
  983.     }
  984. }
  985.  
  986. // Not currently used (for old prototype)
  987. performancingTextEdit.switchStyles = function(aNum) {
  988.     var ourStyleBox = document.getElementById("performancing-main-hbox");
  989.     var ourStyleVBox = document.getElementById("performancing-main-vbox");
  990.     var ourWindow = document.getElementById("transpTest");
  991.     
  992.     switch (aNum){
  993.         
  994.       case "1":
  995.         ourStyleBox.setAttribute('style', "background-image: url('chrome://performancing/skin/bg-temp.png'); background-repeat: repeat-x; ");
  996.         ourStyleVBox.setAttribute('style', "padding: 15px;");
  997.         ourWindow.setAttribute('style', "background-color: none; border: 0;");
  998.         break;
  999.         
  1000.       case "2":
  1001.         ourStyleBox.setAttribute('style', "background-image: url('chrome://performancing/skin/ffextmockup_3.png'); background-repeat: repeat-x; ");
  1002.         ourStyleVBox.setAttribute('style', "padding: 50px;");
  1003.         ourWindow.setAttribute('style', "background-color: white; border: 0;");
  1004.         break;
  1005.         
  1006.       case "3":
  1007.         ourStyleBox.setAttribute('style', "background-image: url('chrome://performancing/skin/bg-temp3.png'); background-repeat: repeat-x; ");
  1008.         ourStyleVBox.setAttribute('style', "padding: 15px;");
  1009.         ourWindow.setAttribute('style', "background-color: none; border: 0;");
  1010.         break;
  1011.         
  1012.       case "4":
  1013.         ourStyleBox.setAttribute('style', "");
  1014.         ourStyleVBox.setAttribute('style', "padding: 15px;");
  1015.         ourWindow.setAttribute('style', "background-color: none; border: 0;");
  1016.         break;
  1017.         
  1018.       case "5":
  1019.         ourStyleBox.setAttribute('style', "background-image: url('chrome://performancing/skin/ffextmockup_3.png'); background-repeat: repeat-x; ");
  1020.         ourStyleVBox.setAttribute('style', "padding: 50px;");
  1021.         ourWindow.setAttribute('style', "background-color: transparent; border: 0;");
  1022.         break;
  1023.         
  1024.       default:
  1025.         var localeString = performancingUI.getLocaleString('styledoesnotexist', []);
  1026.         alert(localeString);
  1027.         break;
  1028.     }
  1029. }
  1030.